 /**
 * v-hasPermi 操作权限处理
 * Copyright (c) 2019 ruoyi
 */

import store from '@/store'
 import {isString} from "@/util";

export const hasPermission = (permissionFlag) => {
  if(!permissionFlag) return true
  if(isString(permissionFlag)) {
    permissionFlag = permissionFlag.split(';')
  }
  const all_permission = "*:*:*";
  const permissions = store.getters && store.getters.permissions
  const hasPermissions = permissions.some(permission => {
    return all_permission === permission || permissionFlag.includes(permission)
  })
  return hasPermissions
}

export default {
  inserted(el, binding, vnode) {
    const { value } = binding

    if (value) {
      const hasPermissions = hasPermission(value)
      if (!hasPermissions) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      return
    }
  }
}
